package com.mycompany.oj.service.Impl;

import com.mycompany.oj.Mapper.Impl.LoginMapperImpl;
import com.mycompany.oj.aspect.GetIp;
import com.mycompany.oj.entity.JsonResult;
import com.mycompany.oj.entity.ResultCode;
import com.mycompany.oj.entity.studentInformation;
import com.mycompany.oj.service.LoginService;
import com.mycompany.oj.util.JwtUtils;
import com.mycompany.oj.util.MessageUtils;
import com.mycompany.oj.util.SharedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author asus
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapperImpl loginMapper;
    @Autowired
    SharedService shaerdService;

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.filepath}")
    private String path;

    @Value("${jwt.picturename}")
    private String pictureName;

    @Value("${jwt.picturepath}")
    private String imagePath;

    private JwtUtils jwtUtils = new JwtUtils();

    //登录
    @Override
    @GetIp
    public List<String> loginService(String username, String password) {
        List list = new ArrayList();
        if (shaerdService.check1(username).equals("空") || shaerdService.check1(password).equals("空")) {
            list.add("账号或密码为空");
            return list;
        }
        List<Map<String, Object>> map = loginMapper.loginMapper(username);

        if (map.isEmpty()) {
            list.add("账号不存在");
            return list;
        } else if (map.get(0).get("passwd").equals(password)) {
            Map<String, Object> newMap = new HashMap<String, Object>();
            Date date = new Date();
            String strDateFormat = "yyyyMMddHHmmss";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            String time = sdf.format(date);
            Long now = null;
            try {
                now = sdf.parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            newMap.put("username", username);
            newMap.put("time", now);
            String jwtToken = jwtUtils.createJWT(newMap, secret);
            list.add(jwtToken);
            return list;
        }
        list.add("密码错误");
        return list;
    }

    //忘记密码
    @Override
    public JsonResult forgetService(String username, String newpassword) {
        List<Map<String, Object>> map = loginMapper.loginMapper(username);
        if (map.isEmpty()) {
            return new JsonResult(ResultCode.FAIL);
        }
        int flag = loginMapper.forgetMapper(username, newpassword);
        if (!shaerdService.check1(String.valueOf(flag)).equals("空")) {
            return new JsonResult(ResultCode.SUCCESS);
        }
        return new JsonResult(ResultCode.FAIL);
    }

    //注册
    @Override
    public boolean registerService(String username, String password) {

        List<Map<String, Object>> a = loginMapper.havenRegister(username);
        if (!a.isEmpty()) {
            return false;
        }
        //添加账号
        String temp[] = loginMapper.registerMapper(username, password, "学生"); //student 的 Account_ID,cellphone

        return shaerdService.check3(temp);
    }

    //注册个人信息
    @Override
    public boolean registerInforamtionService(studentInformation student_information, String cellphone) {
        String nickname = student_information.getNickname();
        String realName = student_information.getStudent_name();
        String sex = student_information.getSex();
        String school = student_information.getSchool();
        String grade = student_information.getGrade();
        String major = student_information.getMajor();
        String schoolId = student_information.getSchoolid();
        String picturePath = pictureName;

        String temp[] = loginMapper.registerInformationMapper(cellphone, nickname, realName, sex, school, grade, major, schoolId, picturePath);
        loginMapper.initFavorite(temp[0]);
        return shaerdService.check3(temp);
    }

    //获取头像
    @Override
    public JsonResult getPictureService(String username) {
        String path = loginMapper.getPictureMapper(username).get(0).get("picture").toString();

        return new JsonResult(ResultCode.SUCCESS, "/" + path);
    }

    //设置头像
    public boolean setPictureService(String username, MultipartFile file) {
        // 要上传的目标文件存放路径
        String Account_id = shaerdService.getAccountId(username);
        String localPath = path;
        String filename = shaerdService.fileUp(Account_id, file, localPath);
        String ppp = imagePath + filename;
        String movefile = loginMapper.getPictureMapperByusername(Account_id).get(0).get("picture_old").toString();
        if (!movefile.equals("0")) {
            File moveFile = new File(movefile);
            moveFile.delete();
        }

        loginMapper.setPictureMapper(Account_id, ppp.replaceFirst(" ", "%20"), localPath + "/" + filename);
        // 上传成功，给出页面提示
        return true;

    }

    @Override
    public JsonResult phoneMa(String phone) throws Exception {
        MessageUtils messageUtils = new MessageUtils();
        return messageUtils.phoneMa(phone);
    }


}
